
package com.airplane.hotdiscuss;

import com.airplane.hotdiscuss.R;
import com.airplane.hotdiscuss.setting.Settings;
import com.airplane.hotdiscuss.ui.AboutActivity;
import com.airplane.hotdiscuss.ui.DemoSwichAnimationActivity;
import com.airplane.hotdiscuss.ui.MyWebView;
import com.airplane.hotdiscuss.ui.SlidingDrawerDemo;
import com.airplane.hotdiscuss.util.Constants;
import com.airplane.hotdiscuss.util.MyLogger;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Main extends Activity {
    private static final MyLogger logger = MyLogger.getLogger(Main.class.getSimpleName());

    private static final int MENU_CONFIG = 1;
    private static final int MENU_ABOUT = 2;
    private static final String URL_BAIDU = "http://m.baidu.com";
    private static final String URL_GOOGLE = "http://m.google.com";

    private EditText mEditText;
    private Button mButton;
    private String mWebUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mEditText = (EditText) findViewById(R.id.edit_text);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mWebUrl = mEditText.getText().toString();
                logger.d("onClick(), send text : " + mWebUrl);
                if (mWebUrl.length() > 0) {
                    if (!mWebUrl.contains("http")) {
                        mWebUrl = "http://" + mWebUrl;
                        logger.d("Regular web url is : " + mWebUrl);
                    }
                } else {
                    return;
                }

                Intent intent = new Intent(Main.this, MyWebView.class);
                intent.putExtra(Constants.INTENT_KEY_WEBURL, mWebUrl);
                startActivity(intent);
            }
        });

        findViewById(R.id.button_to_baidu).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main.this, MyWebView.class);
                intent.putExtra(Constants.INTENT_KEY_WEBURL, URL_BAIDU);
                startActivity(intent);
            }
        });

        findViewById(R.id.button_to_google).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main.this, SlidingDrawerDemo.class);
                //Intent intent = new Intent(Main.this, MyWebView.class);
                //intent.putExtra(Constants.INTENT_KEY_WEBURL, URL_GOOGLE);
                startActivity(intent);
            }
        });

        findViewById(R.id.button_demo_switch_anim).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
               startActivity(new Intent(Main.this, DemoSwichAnimationActivity.class));
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, MENU_CONFIG, 0, R.string.menu_config).setIcon(
                android.R.drawable.ic_menu_preferences);
        menu.add(0, MENU_ABOUT, 0, R.string.menu_about).setIcon(
                android.R.drawable.ic_menu_info_details);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        Intent in = new Intent();
        switch (item.getItemId()) {
            case MENU_CONFIG:
                in.setClass(this, Settings.class);
                startActivity(in);
                break;
            case MENU_ABOUT:
                in.setClass(this, AboutActivity.class);
                startActivity(in);
            default:
                return super.onOptionsItemSelected(item);
        }

        return true;
    }
}
